define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'orders/orders/index',
                    add_url: 'orders/orders/add',
                    edit_url: 'orders/orders/edit',
                    del_url: 'orders/orders/del',
                    multi_url: 'orders/orders/multi',
                    table: 'orders',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        // {field: 'user_id', title: __('User_id')},
                        {field: 'users.phone', title: '用户手机号'},
                        {field: 'users.truename', title: '用户真实姓名'},

                        {field: 'order_num', title: __('Order_num')},
                        {field: 'status', title: __('Status'), searchList: {"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3'),"4":__('Status 4'),"6":__('Status 6')}, visible:false},
                        {field: 'status_text', title: __('Status'),operate:false,formatter: Table.api.formatter.normal},
                        // {field: 'num', title: __('Num')},
                        {field: 'total_money', title: __('Total_money'), operate:'BETWEEN'},
                        {field: 'names', title: __('Names')},
                        {field: 'phone', title: __('Phone')},
                        {field: 'address', title: __('Address')},
                        {field: 'logistics', title: __('Logistics')},
                        {field: 'logistics_code', title: __('Logistics_code')},
                        // {field: 'logistics_code', title: __('Logistics_code')},
                        // {field: 'logistics', title: __('Logistics'),operate: false,
                        //     formatter: function (value, row, index) {
                        //         return __('logisticsType_home')[row.logistics_type];
                        //     }
                        // },
                        {field: 'pament_time', title: __('Pament_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'pay_style', title: __('Pay_style'), searchList: {"1":__('Pay_style 1'),"2":__('Pay_style 2')}, formatter: Table.api.formatter.normal},
                      /*  {field: 'use_integral', title: __('Use_integral'), operate:'BETWEEN'},*/
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange'},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange'},
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                        {field: 'btown', title: __('订单详情'), table: table, buttons: [
                                {
                                    name: '订单详情',
                                    text: '商品列表',
                                    title: '商品列表',
                                    icon: 'fa fa-list',
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    url: 'orders/orders/detail',
                                    callback: function (data) {
                                    }
                                },
                            ], operate: false, formatter: Table.api.formatter.buttons
                        },
                        {field: 'operate', title: '', table: table, buttons: [
                                {
                                    name: 'fh',
                                    text: '发货',
                                    title: '发货',
                                    icon: 'fa fa-flash',
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    url: 'orders/orders/fh',
                                }
                            ],events: Table.api.events.operate, formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                if(row.status == 1 || row.status == 3|| row.status == 4|| row.status == 5|| row.status == 6){
                                    $(table).data("operate-fh", null);
                                    $(table).data("operate-sh", null);
                                }
                                $(table).data("operate-edit", null);
                                $(table).data("operate-del", null);
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        }
                        ]
                ]
            });


            // 绑定TAB事件
            $('.panel-heading a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var field = $(this).closest("ul").data("field");
                var value = $(this).data("value");
                var options = table.bootstrapTable('getOptions');
                options.pageNumber = 1;
                options.queryParams = function (params) {
                    var filter = {};
                    if (value !== '') {
                        filter[field] = value;
                    }
                    params.filter = JSON.stringify(filter);
                    return params;
                };
                table.bootstrapTable('refresh', {});
                return false;
            });
            //导出
            var submitForm = function (ids, layero) {
                var options = table.bootstrapTable('getOptions');
                console.log(options);
                var columns = [];
                $.each(options.columns[0], function (i, j) {
                    if (j.field && !j.checkbox && j.visible && j.field != 'operate') {
                        columns.push(j.field);
                    }
                });
                var search = options.queryParams({});
                $("input[name=search]", layero).val(options.searchText);
                $("input[name=ids]", layero).val(ids);
                $("input[name=filter]", layero).val(search.filter);
                $("input[name=op]", layero).val(search.op);
                $("input[name=columns]", layero).val(columns.join(','));
                $("form", layero).submit();
            };
            $(document).on("click", ".btn-export", function () {
                var ids = Table.api.selectedids(table);
                var page = table.bootstrapTable('getData');
                var all = table.bootstrapTable('getOptions').totalRows;
                console.log(ids, page, all);
                Layer.confirm("请选择导出的选项<form action='" + Fast.api.fixurl("orders/orders/export") + "' method='post' target='_blank'><input type='hidden' name='ids' value='' /><input type='hidden' name='filter' ><input type='hidden' name='op'><input type='hidden' name='search'><input type='hidden' name='columns'></form>", {
                    title: '导出数据',
                    btn: ["选中项(" + ids.length + "条)", "本页(" + page.length + "条)", "全部(" + all + "条)"],
                    success: function (index,layero) {
                        // alert(111);
                        $(".layui-layer-btn a", layero).addClass("layui-layer-btn0");
                        $(".btn-refresh").trigger("click");//刷新当前页面的数据
                    },
                    yes: function (index, layero) {
                        submitForm(ids.join(","), layero);
                        return true;
                    },
                    btn2: function (index, layero) {
                        var ids = [];
                        $.each(page, function (i, j) {
                            ids.push(j.id);
                        });
                        submitForm(ids.join(","), layero);
                        return true;
                    },
                    btn3: function (index, layero) {
                        submitForm("all", layero);
                        return true;
                    }
                });
                parent.Layer.close(index);
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        fh: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});